package cn.com.jslh.boarpro.main.presenter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimAdapterEx;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.main.contract.SearchItemInfoContract;
import cn.com.jslh.boarpro.main.model.SearchItemInfoModelImpl;
import cn.com.jslh.boarpro.util.JSONUtil;
import cn.com.jslh.boarpro.util.WebService;

/**
 * Created by Administrator on 2017/11/13.
 */

public class SearchItemInfoPresenterImpl implements SearchItemInfoContract.Presenter {

    private Context context;
    private SearchItemInfoContract.View view;
    private SlimAdapterEx adapter;
    private String fid;
    private SearchItemInfoContract.Model model;

    public SearchItemInfoPresenterImpl(Context context, SearchItemInfoContract.View view) {
        view.setPresenter(this);
        this.view = view;
        this.context = context;
        this.model = new SearchItemInfoModelImpl();
    }

    @Override
    public void start() {
        if (fid == null)
            return;
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("searchType", SearchPresenterImpl.SearchType);
            jsonObject.put("fid", fid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        model.getDatas(context, jsonObject, new WebService.NetSuccessListener() {
            @Override
            public void Success(String s) throws JSONException {
                JSONArray jsonArray = new JSONArray(s);
                if (jsonArray == null || jsonArray.length() == 0)
                    return;
                adapter.updateData(JSONUtil.JsonArray2List(jsonArray)).notifyDataSetChanged();
            }
        });
    }

    @Override
    public void bindRV(RecyclerView recyclerView) {
        adapter = SlimAdapter.create(SlimAdapterEx.class)
                .enableDiff()
                .enableEmptyView(recyclerView.getContext(), R.layout.empty_layout)
                .registerDefault(R.layout.search_item_sinfo_layout, new SlimInjector() {
                    @Override
                    public void onInject(Object data, IViewInjector injector) {
                        try {
                            String[] dataStrs = ((JSONObject) data).getString("data").split("：");
                            if (dataStrs.length < 2) {
                                injector.text(R.id.titleText, dataStrs[0] + "：");
                            }else {
                                injector.text(R.id.valueText, dataStrs[1]).text(R.id.titleText, dataStrs[0] + "：");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }).attachTo(recyclerView);
    }

    @Override
    public void setFid(String fid) {
        this.fid = fid;
    }
}
